#pragma once
#ifndef __VMATERIALMESH_H__
#define __VMATERIALMESH_H__
#include "VMaterialCommon.h"
// Material for mesh objects. 


// shader parameters for mesh (static / skin) material.
class VCoreApi VMeshMaterialShaderParamters : public VMaterialShaderParameters
{
protected:
	virtual void RT_AttachShader(VShader* Shader);
	virtual void RT_DeleteThis();
public:
	inline VMeshMaterialShaderParamters()
	{
	}
	virtual ~VMeshMaterialShaderParamters()
	{

	}
	VShaderParameter WorldTransform;		// parameter for mesh

	VShaderParameter MaterialProperty;		// x : Alpha Ref

	VShaderParameter DiffuseColor;
	VShaderTextureParameter DiffuseTex;		// DiffuseTextureSampler.
	VShaderTextureParameter NormalTex;
};


// base class for mesh material.
class VCoreApi VMeshMaterial : public VMaterial
{
public:
	VMeshMaterial(VMaterialTemplatePtr Template);
protected:
	// parameters.
	virtual VMaterialShaderParameters* CreateShaderParamters(EVRenderPass RenderPass) const;

	virtual void RT_SetMeshParameters(const VMaterialInstanceCommitContext& CommitContext);
	virtual void RT_SetShaderParameters(const VMaterialInstanceCommitContext& CommitContext);
	virtual void RT_SetShaderParameters_PrePass(const VMaterialInstanceCommitContext& CommitContext);
	virtual void RT_SetShaderParameters_ShadowGen(const VMaterialInstanceCommitContext& CommitContext);

	virtual const VVector4& RT_GetParameterValue(const VRefString& ParameterName) const;
	virtual VRTexture* RT_GetTextureValue(const VRefString& TextureName) const;

protected:
	virtual BOOL Load(const VXMLElement* Element);
	virtual BOOL Save(VXMLElement* Element) const;

protected:
	virtual BOOL InitRenderData();
	VMaterialParameter m_DiffuseColorParameter;
	VVector4 m_MaterialProperty;

	VMaterialTexture* m_DiffuseTexture;
	VMaterialTexture* m_NormalTexture;
	VMaterialTexture* m_DetailTexture;

};

// base class for mesh 's material template. 
class VCoreApi VMaterialMeshTemplate : public VCommonMaterialTemplate
{
public:
	VMaterialMeshTemplate();
	virtual ~VMaterialMeshTemplate(void);
	virtual const char* GetName() { return "MeshBase"; }
	virtual VMaterial* CreateMaterial() { return VNew VMeshMaterial(this);}

	virtual void RT_SetMaterialShaderParameters(VMaterialInstance* MaterialInstance, VShader* Shader, VMaterialShaderParameters* ShaderParameters);
protected:
	virtual BOOL InitTemplate();
	virtual void ShutdownTemplate();
	virtual BOOL IsSupportRenderMesh(EVRenderMeshType RenderMeshType) const 
	{ 
		//return RenderMeshType == VRMT_STATIC_MESH || RenderMeshType == VRMT_SKIN; 
		return RenderMeshType == VRMT_STATIC_MESH; 
	}
	virtual void SetupCompileContext(VShaderCompileContext& CompileContext) const;
	virtual const char* GetVertexCode() { return GetCachedVertexCode("Mesh/VMeshBaseV.h"); }
	virtual const char* GetPixelCode() { return GetCachedPixelCode("Mesh/VMeshBaseP.h"); }
};

#endif 
